package org.sucri.floxs.ext.form;

import org.sucri.floxs.utils.Json_Util;
import static org.sucri.floxs.utils.Json_Util.*;
import org.sucri.floxs.ext.widget.ExtWidget;
import org.sucri.floxs.Tools;

import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Wen Yu
 * Date: Aug 19, 2007
 * Time: 4:34:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class Form extends ExtWidget
{
  private static final String  LABEL_ALIGN = "labelAlign";
  private static final String BUTTON_ALIGN = "buttonAlign";
  private static final String  LABEL_WIDTH = "labelWidth";
  private static final String          URL = "url";

  private String           mContainer = null;
  private List<FormField>  mFields  = null;
  private List<FormButton> mButtons = null;

  public Form() { super(); init(); }
  public Form(String url) { super(); init(); setURL(url); }

  public void setURL(String s) { if (Tools.good(s)) mConfig.put(URL, s); }

  public void addField(FormField s)
  {
    if (mFields == null) mFields = new ArrayList<FormField>();
    mFields.add(s);
  }
  public void addButton(FormButton s)
  {
    if (mButtons == null) mButtons = new ArrayList<FormButton>();
    mButtons.add(s);
  }
  public void setLabelAlign( String s) { mConfig.put(LABEL_ALIGN,  s); }
  public void setButtonAlign(String s) { mConfig.put(BUTTON_ALIGN, s); }
  public void setLabelWidth( int    s) { mConfig.put(LABEL_WIDTH,  s); }

  private void init()
  {
    setLabelAlign("right");
    setLabelWidth(150);
    setButtonAlign("right");
  }
  /**
   *
   * @author Shea Frederick
   * 
Ext.onReady(function(){

	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

    var form_employee = new Ext.form.Form({
        labelAlign: 'left',
        labelWidth: 175,
		buttonAlign: 'right'
    });

    var employee_name = new Ext.form.TextField({
        fieldLabel: 'Name',
        name: 'name',
        width:190
    });

    var employee_title = new Ext.form.TextField({
        fieldLabel: 'Title',
        name: 'title',
        width:190
    });

    var employee_hire_date = new Ext.form.DateField({
        fieldLabel: 'Hire Date',
        name: 'hire_date',
        width:90,
        allowBlank:false,
		format:'m-d-Y'
    });

	var employee_active = new Ext.form.Checkbox({
        fieldLabel: 'Active',
        name: 'active'
    });

    form_employee.fieldset(
        {legend:'Employee Edit'},
		employee_name,
		employee_title,
		employee_hire_date,
		employee_active
	);

	form_employee.addButton('Save', function(){
		if (form_employee.isValid()) {
			Ext.MessageBox.alert('Success', 'You have filled out the form correctly.');
		}else{
			Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
		}
	}, form_employee);

	form_employee.render('employee-form');

});
   * @return
   */
  public String initFunc(String var, String dom)
  {
    setVarName(var);
    
    String f = render(var);

    if (Tools.good(mFields))
    {
      f += var + ".add(" + br();
      String args = null;
      for (FormField ff : mFields)
      {
        args = Tools.extend(args, ff.construct(), ",");
      }
      f += args + ");" + br();
    }
    if (Tools.good(mButtons))
    {
      for (FormButton fb : mButtons)
      {
        f += var + ".addButton('" + fb.getName() + "');" + br();        
      }
    }
    f += var + ".render('" + dom + "');";

    f = "Ext.form.Field.prototype.msgTarget = 'side';" + br() + f;
    return "function init_form() {" + br() + f + "};" + br();
  }
  public String construct()
  {
    return newJs("Ext.form.Form", toJSONString(mConfig));
  }
  protected String getSubmitFunction()
  {
    return funcJs(
        ifJs(callJs(getVarName(), "isValid"),
             callJs(getVarName(), "submit", "params"),
            "Ext.MessageBox.alert('Errors', 'Please fix the errors noted.')"));
  }
  
}
